package com.net.xpay.clean.scheduler;


import com.net.common.util.LocalDateUtil;
import com.net.xpay.common.domain.UserShard;
import com.net.xpay.common.domain.stat.UserDailyStat;
import com.net.xpay.common.manager.UserShardManager;
import com.net.xpay.common.manager.stat.UserDailyStatManager;
import com.net.xpay.core.enums.SafePayOrderStatus;
import com.net.xpay.core.manager.PayTransManager;
import com.net.xpay.core.manager.safePay.SafePayOrderManager;
import com.net.shard.ShardUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 2019/7/15 16:28

 * 修复或者初始化数据
 */
@Slf4j
//@Component
public class UserDailyStatAddColumnScheduler {

    @Autowired
    private PayTransManager payTransManager;

    @Autowired
    private SafePayOrderManager safePayOrderManager;

    @Autowired
    private UserDailyStatManager userDailyStatManager;

    @Autowired
    private UserShardManager userShardManager;

    @Scheduled(cron = "0 50 19 * * ?")
    public void updateCompanyDailyStat() {
        LocalDate startTime = LocalDate.of(2023,11,1);
        LocalDate endTime = LocalDate.now().minusDays(1L);

        while (!startTime.isAfter(endTime)) {
            List<UserDailyStat> list = userDailyStatManager.listByDay(startTime);
            for (UserDailyStat stat : list) {
                stat.setTotalSuccessLargeCount(0L);

                stat.setPayTransSuccessLargeCount(0L);
                stat.setSafePayTransSuccessLargeCount(0L);
                statUserDailyStat(stat);
            }
            startTime = startTime.plusDays(1L);
        }


    }

    private void statUserDailyStat(UserDailyStat stat) {
        LocalDate day = stat.getDay();
        // 获取当天的起始时间和终止时间 终止时间保留在23:59:59.999999999
        LocalDateTime minEndTime = LocalDateUtil.getDayStart(day);
        LocalDateTime maxEndTime = LocalDateUtil.getDayEnd(day);
        //统计数据并入库
        UserShard userShard = userShardManager.getById(stat.getUserId());
        try {
            ShardUtil.setShardContext(userShard.getShardId());
            saveUserDailyStat(minEndTime ,maxEndTime, stat,stat.getUserId());
        } finally {
            ShardUtil.cleanShardContext();
        }

        userDailyStatManager.update(stat);
    }



    private void saveUserDailyStat(LocalDateTime minEndTime, LocalDateTime maxEndTime, UserDailyStat stat,Long userId) {
        //批量有密单笔超过10000金额成功笔数
        long payTransSuccessLargeCount = payTransManager.statUserLargeCount(userId,minEndTime, maxEndTime);
        //安全发单笔超过10000金额成功笔数
        long safePayTransSuccessLargeCount = safePayOrderManager.statUserLargeCount(userId,minEndTime, maxEndTime, SafePayOrderStatus.SUCCESS);

        //批量有密单笔超过10000金额成功笔数
        stat.setPayTransSuccessLargeCount(payTransSuccessLargeCount);
        //安全发单笔超过10000金额成功笔数
        stat.setSafePayTransSuccessLargeCount(safePayTransSuccessLargeCount);
        //单笔超过10000金额成功笔数 总和数据
        stat.setTotalSuccessLargeCount(stat.getPayTransSuccessLargeCount() + stat.getSafePayTransSuccessLargeCount());
    }

}
